
var $day = $("#day"),
    $hour = $("#hour"),
    $minute = $("#minute"),
    $second = $("#second");

var $totalNum = $("#totalNum"),
    $startingPrice = $("#startingPrice"),
    $discountPrice = $("#discountPrice"),
    $schedule = $("#schedule");

var $had = $("#had");
    $point = $("#point"),   
    $members = $("#members"),
    $decade = $("#decade"),
    $unit = $("#unit");

var $payAttaction = $("#payAttaction"),
    $wrapper = $("#wrapper");  

var $createElement = function (tag) {
    var t = tag.split('.');
    return $(document.createElement(t[0])).addClass(t[1] || "");
};

/*get json*/
$.ajax({
    //url:"http://otbtsnw47.bkt.clouddn.com/zjyc_data.json?v=20170925",
    url:"zjyc_data.json",
    type:"GET",
    dataType:"json",
    success:function (response){
        //页面填充数据
        opElemHtmlData(response);
        //进度条
        loading(response.data);
        //启动倒计时
        setInterval(function(){
            countDown(
                response.data.deadline[0], 
                response.data.deadline[1], 
                response.data.deadline[2], 
                response.data.deadline[3], 
                response.data.deadline[4], 
                response.data.deadline[5]
                );
        }, 1000); 
    },  
    error: function(response){
        alert("加载活动数据失败，code:", response.status)
    }
});


/*倒计时功能*/
function countDown(year, month, day, hour, minute, second){ 

    //剩余的毫秒数
    var leftTime = (new Date(year, month - 1, day, hour, minute, second).getTime() - (new Date().getTime()));
    
    //剩余的天数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10);
    //剩余的小时 
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10);
    //剩余的分钟
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);
    //剩余的秒数
    var seconds = parseInt(leftTime / 1000 % 60, 10);

    days = doubleDigit(days); 
    hours = doubleDigit(hours); 
    minutes = doubleDigit(minutes); 
    seconds = doubleDigit(seconds);

    
    $day.html(days);
    $hour.html(hours);
    $minute.html(minutes);
    $second.html(seconds);
} 

//页面数据操作
function opElemHtmlData(data){
    var response = data.data;
    //解析数据
    var totalNum = response.totleNum,
        startingPrice = response.ourPrices,
        discountPrice = response.successPrice,
        ourPrices = response.ourPrices,
        buyoutNum = response.buyoutNum;

    $members.html(initZero(Number(data.data.joinPeople)));
    $totalNum.html(totalNum);
    $startingPrice.html(startingPrice);
    $discountPrice.html(discountPrice);

    //计算现在价格 显示在标牌上
    var nowPrice = Math.floor(Number(ourPrices) - Number(buyoutNum) * 0.55);
    //最低为5元
    nowPrice >= 5 ? nowPrice = nowPrice : nowPrice = 5;

    var nowPriceArrage = nowPrice.toString().split("");

    priceAnimation(nowPriceArrage)

}

//价格条动画
function priceAnimation(arr){
    //归零
    $decade.css({
        "top":0
    });
    $unit.css({
        "top": 0
    })
    //动画
    if(arr.length == 2){
        $decade.css({
            "top": -arr[0] * 61.4
        })
        $unit.css({
            "top": -arr[1] * 61.4
        })
    }else{
        //只剩一位数
       $decade.css({
            "left": 0,
            "top": 0
        })
        $unit.css({
            "top": -arr[0] * 61.4
        })
    }
}

//单位数补零
function doubleDigit(number){ 

    if(number < 10){ 
        number = "0" + number; 
    } 
    return number; 
}

//单位补zero 
function initZero(number){
    if(number < 10){
        number = "000" + number; 
    }else if(number => 10 && number < 100){
        number = "00" + number;
    }else if(number >= 100 && number < 1000){
        number = "0" + number;
    }else{
        number = number;
    }
    return number;
}

//qcode 关注二维码
$payAttaction.bind("touchstart mousedown", function(){
    layer.open({
        content:'<img class="qcode" src="img/qcode.png">',
        area:["80%","44%"],
        closeBtn: 1,
        btn:[],
        anim: 1,
        title: 0, 
        shadeClose: true 
    });
    event.preventDefault();
})



$("#joinTo").on("click", function(){
    layer.open({
        content:'<div class="tips"><h3 class="title">友情提示</h3><p>本页面数据更新时间为10:00-22:00（每2小时更新一次）</p></div><a class="accept" href="https://weidian.com/i/2165548710">我知道了</a>',
        area:["80%","30%"],
        closeBtn: 0,
        btn:[],
        anim: 1,
        title: 0, 
        shadeClose: true 
    })
})


//初始化分辨率
function screens(w, h){
    $("#wrapper").css({
        "width": w,
        "height": h
    })
}

screens(window.innerWidth, window.innerHeight);




